// pages/material/index/index.js
let api = require("../../../api/materialApi")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardCur: 0,
    swiperList: [],
    categoryList: [],
    next: null,
    materialList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    api.getMaterialBanner().then(res=>{
      console.log(res)
      that.setData({ swiperList: res.data })
    })

    api.getMaterialCategory().then(res=>{
      console.log(res)
      that.setData({ categoryList: res.data })
    })

    api.getNewMaterial(null).then(res=>{
      console.log(res)
      that.setData({
        materialList: res.data.results,
        next: res.data.next
      })
      console.log(that.data.materialList)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    if(that.data.next == null){

      wx.showToast({
        title:"没有更多了",
        icon: 'none',
        duration: 2000
      })

    }else{
      api.getNewMaterial(that.data.next).then(res=>{
        let results = res.data.results
        let _materialList = that.data.materialList
        for(let i = 0; i < results.length; i++){
          _materialList.push(results[i])
        }
        that.setData({
          materialList: _materialList,
          next: res.data.next
        })
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

    // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },

  toMaterialDetial: function(e){
    console.log(e)
    let material_id = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/material/materialDetail/materialDetail?material_id='+material_id,
    })
  },

  toUserMaterial: function(){
    wx.navigateTo({
      url: '/pages/material/userMaterial/userMaterial',
    })
  }
})